home comics writing pictures archive about

WindowViewModelBase.cs

Language: C#
Last Modified: 2020-06-27 1:58:37 PM UTC
File Size: 3300 bytes
http://www.penguinstew.ca/example/MVVMbase/ViewModelBase/WindowViewModelBase.cs
using System;
using System.ComponentModel;
using System.Windows;
namespace Penguin.MVVMBase.ViewModelBase
{
public class WindowViewModelBase : ValidationViewModelBase, IWindowViewModel
{
#region IWindowViewModel
/// <summary>
/// Event raised to indicate implementing viewModel is requesting a view
/// </summary>
public event EventHandler<RequestViewArgs> ViewRequested;
/// <summary>
/// Event raised to indicate implementing viewModel is requesting a message box be shown
/// </summary>
public event EventHandler<RequestMsgBoxArgs> MsgBoxRequested;
/// <summary>
/// Method called when the view is loaded
/// </summary>
/// <param name="sender">The sender object as passed to the loaded event handler</param>
/// <param name="e">RoutedEventArgs as passed to the loaded event handler</param>
public virtual void ViewLoaded(object sender, RoutedEventArgs e)
{
//Default implementation: do nothing
return;
}
/// <summary>
/// Method called when a requested view has closed
/// </summary>
/// <param name="viewModel">The ViewModel of the closed view</param>
/// <param name="result">Return value of the view</param>
public virtual void RequestedViewClosed(IWindowViewModel viewModel, bool result)
{
//Default implementation: do nothing
return;
}
#endregion
#region Protected Methods
/// <summary>
/// Raises the ViewRequested event with the given viewModel
/// </summary>
/// <param name="viewModel">ViewModel to request a view for</param>
protected void RequestView(IWindowViewModel viewModel)
{
EventHandler<RequestViewArgs> handler = ViewRequested;
if (handler != null)
{
handler(this, new RequestViewArgs(viewModel));
}
}
/// <summary>
/// Raises the RequestMsgBox event with the given parameters
/// </summary>
/// <param name="messageBoxText">The text to display in the MessageBox</param>
/// <param name="messageBoxCaption">The caption to display in the MessageBox (default "")</param>
/// <param name="messageBoxButtons">The buttons to display in the MessageBox (default OK)</param>
/// <param name="messageBoxIcon">The image to display in the MessageBox (default None)</param>
/// <param name="callback">The action to call with the MessageBox result (default null)</param>
protected void RequestMsgBox(string messageBoxText, string messageBoxCaption = "", MessageBoxButton messageBoxButtons = MessageBoxButton.OK,
MessageBoxImage messageBoxIcon = MessageBoxImage.None, Action<MessageBoxResult> callback = null)
{
EventHandler<RequestMsgBoxArgs> handler = MsgBoxRequested;
if (handler != null)
{
handler(this, new RequestMsgBoxArgs(messageBoxText, messageBoxCaption, messageBoxButtons, messageBoxIcon, callback));
}
}
#endregion
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80